<template>
  <el-dialog
    title="选择流程"
    width="400px"
    :visible.sync="params.visible"
  >
    <compTree
      ref="tree"
      :show-search="false"
      :comp-tree-height="350"
      :tree-default-expanded-keys="treeDefaultExpandedKeys"
      :tree-data="treeData"
    />
    <dialog-btn slot="footer" />
  </el-dialog>
</template>

<script>
import { getFlowList } from '@/api/flow'
import { SQLTable } from '../../../public/js/paramarr'
import Global from '../../../public/js/global'
import DialogBtn from './DialogBtn.vue'
export default {
  components: { DialogBtn },
  props: {
    params: {
      type: Object,
      default() {
        return {
          visible: false,
          id: ''
        }
      }
    }
  },
  data() {
    return {
      treeData: [],
      treeDefaultExpandedKeys: []
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      getFlowList().then((vPar) => {
        const nodes = []
        const nodeMap = new Map()
        let table = new SQLTable(vPar.getTable('k_flow_group'))
        table.first()
        while (!table.eof()) {
          const node = {}
          const pnode = Global.getTreeviewPNode(
            nodeMap,
            nodes,
            table.getValueByName('PID')
          )
          node['id'] = table.getValueByName('ID')
          node['text'] = table.getValueByName('Name')
          // node['icon'] = 'folder'
          node['folder'] = true
          pnode.push(node)
          nodeMap.set(node.id, node)
          table.next()
        }
        table = new SQLTable(vPar.getTable('k_flow_info'))
        table.first()
        while (!table.eof()) {
          const node = {}
          const pnode = Global.getTreeviewPNode(
            nodeMap,
            nodes,
            table.getValueByName('PID')
          )
          node['id'] = table.getValueByName('FlowID')
          node['pid'] = table.getValueByName('PID')
          node['name'] = table.getValueByName('FlowName')
          node['text'] = node['name']
          node['icon'] = 'liucheng'
          node['audit'] = table.getValueByName('Audit')
          node['folder'] = false
          pnode.push(node)
          nodeMap.set(node.id, node)
          table.next()
        }
        this.treeData = nodes
        if (this.params.id) {
          this.$refs.tree.setCurrentKey(this.params.id)
          this.treeDefaultExpandedKeys.push(this.params.id)
        }
      })
    },
    confirm() {
      const node = this.$refs.tree.getCurrentNode()
      if (node === null) {
        return this.$message.warning('请选择流程')
      }
      // if (!this.node.folder && this.node.audit !== '1') return this.$message.warning('流程未审核， 请先审核流程')
      if (this.params.confirm !== undefined) {
        this.params.confirm(node)
      } else {
        this.$emit('confirm', node)
      }
    }
  }
}
</script>

<style>
</style>
